<!--Beyond Scripts-->
<script src="/xinhuashudian/public/admin/js/beyond.min.js"></script>
<!--<script src="/admin/js/fuelux/spinner/fuelux.spinner.min.js"></script>-->

<script type="text/javascript">
    //展开选中效果
    var data_href;
    $('.del-btn').on('click', function () {
        $('#modal-delete').modal('show');
        data_href = $(this).attr('data-href');
        $('#modal-delete .btn-ok').click(function () {
            window.location.href = data_href;
            $('#modal-delete').modal('hide');
        });
    });

    $('.action-btn').on('click', function () {
        $('#modal-action').modal('show');
        data_href = $(this).attr('data-href');
    });
    $('#modal-action .btn-ok').click(function () {
        window.location.href = data_href;
        $('#modal-action').modal('hide');
    });
    //同步
    $.ajaxSetup({
        async: false
    });

    //收银台相关
    //监听并获取会员卡信息
    function getCard() {
        var keyCode = null;

        if (event.which)
            keyCode = event.which;
        else if (event.keyCode)
            keyCode = event.keyCode;

        if (keyCode == 13) {
            getCard_post();
        }
    }
    //获取会员卡信息
    function getCard_post() {
        var $card_id = $("#card_id").val();
        if ($.trim($card_id) == "") {
            $("#card_id").val("");
            $("#card_id").focus();
            return;
        }
        $.post("{:U('Card/getinfo')}", {"card_id": $card_id}, function (data) {
            $("#card_info .info").html("");
            $(".error_info").html("");
            if (data.status == "error") {
                $str = '<span class="info">此卡不存在</span><br>';
                $("#card_info").html($str);

                $("#card_info .info").html("此卡不存在");
                $(".error_info").html("此卡不存在");
                $("#user_id").val("");
                $("#card_info").show();
                $("#card_id").focus();
            } else {
                $str = '<span class="info">查询成功</span><br>' +
                        '小孩姓名：<span class="child_name">' + data.data.child_name + '</span><br>' +
                        '会员有效期：<span class="create_time">' + data.data.create_time + '</span>~<span class="expire_time">' + data.data.expire_time + '</span><br>' +
                        '购书折扣：<span class="discount_buy">' + data.data.discount_buy + '</span><br>' +
                        '活动报名费折扣：<span class="discount_apply">' + data.data.discount_apply + '</span><br>' +
                        '{:C("rent_freenum_name")}：<span class="num_delivery">' + data.data.num_delivery + '</span><br>' +
                        '{:C("act_freenum_name")}：<span class="num_activity">' + data.data.num_activity + '</span><br>' +
                        '冻结时间段：<span class="">' + data.data.delay_time.start_time + '~' + data.data.delay_time.end_time + '</span><br>' +
                        '状态：<span class="is_lock">' + data.data.is_lock + '</span><br>' +
                        '备注：<span class="remark">' + data.data.remark + '</span><br>';
                $("#card_info").html($str);
                $("#card_info").show();
                //会员过期提示
                if(data.data.is_overdue ==1){
                    alert('会员已过期,无法借阅等操作');
                }
                getsuccess(data);
            }
            cal_all();
        }, "json");
    }
    var data;
    //显示图书搜索选择模态框
    function getSelectBook($data) {
        var $booklist = $('#modal-book-selector [name="booklist"]');
        data = $data;
        //默认不选中，强制用户选择
        $booklist.html("");
        if(data.data.length>1){
            $booklist.prepend('<option value="-1" selected="selected">--请选择--</option>');
        }
        $.each(data.data, function (i, item) {
            if (item.order_id == null) {
                item.order_id = "";
            }
            $str = '<option value="' + i + '">' +
                    'ID：' + item.order_id + '/' + item.book_id + '-' +
                    '书名：' + item.sm + '-' +
                    '作者：' + item.zyz + '-' +
                    '定价：￥<span class="dj">' + item.dj + '</span>元&nbsp;&nbsp;' +
                    '' +
                    '</option>';
            $booklist.prepend($str);
        });
        $('#modal-book-selector').show();
        $('#modal-book-selector .booknum').html(data.data.length);
        $('#modal-book-selector .btn-ok').click(function () {
            //选择后回调
            var $i = $booklist.val();
            if ($i == null || $i=='-1') {
                $booklist.html("");
                $('#modal-book-selector').modal('hide');
                return;
            }
            $('#modal-book-selector').modal('hide');
            $booklist.html("");
            doadd(data.data[$i]);
        });
        $booklist.keydown(function () {
            var keyCode = null;

            if (event.which)
                keyCode = event.which;
            else if (event.keyCode)
                keyCode = event.keyCode;

            if (keyCode == 13) {
                $('#modal-book-selector .btn-ok').click();
            }
        });
        $booklist.focus();
        $booklist.click();
        $('#modal-book-selector').modal('show');
    }
    //日志输出
    function log($info) {
        console.debug($info);
    }
    var bdhtml;
    var prnhtml;
    //打印
    function printOrder(flag) {
        bdhtml = window.document.body.innerHTML; //获得body标签内的全部html代码
        sprnstr = '<!--printstart' + flag + '-->'; //声明一个字符串，用于表示打印的起始位置
        eprnstr = '<!--printend' + flag + '-->'; //标示打印的结束位置
        prnhtml = bdhtml.substr(bdhtml.indexOf(sprnstr) + 18); //查找打印的起始字符串并加上17，因为的长度为17，所以要加17，这样打印时不会把标示打印出来
        prnhtml = prnhtml.substring(0, prnhtml.indexOf(eprnstr)); //和上边的意思差不多
        if (prnhtml.length <= 0) {
//            无可打印内容
            return;
        }
        prnhtml = '<h1 style="font-family:STXingkai;">新华书店</h1>' +
                '<p class="title">小票</p>' + prnhtml;
        window.document.body.innerHTML = prnhtml; //通过window对象得到body对象并将当前窗体的内容设置为要打印起始标示符和结束标示符之间的内容，这样打印出来就只有需要的部分，而其他没用的信息都不会有的
        window.print();
        setTimeout(function () {
            window.document.body.innerHTML = bdhtml;
            $(".loading-container").addClass("loading-inactive");
        }, 10);
//        window.document.body.innerHTML=bdhtml;
    }

    //自动检查号码并格式化
    function checkphone($v) {
        $v = $v.replace(/\s+/g, "");
        var $len = $v.length;
        var $str = $v.substr(0, 11);
        var $str_1 = $str.substr(0, 3);
        var $str_2 = $str.substr(3, 4);
        var $str_3 = $str.substr(7, 4);
        var $val = '';
        if ($len < 4) {
            $val = $str_1;
        }
        if ($len > 3 && $len < 8) {
            $val = $str_1 + " " + $str_2;
        }
        if ($len > 7) {
            $val = $str_1 + " " + $str_2 + " " + $str_3;
        }
        return $val;
    }

    $(function () {
        $('#sidebar_cashier li').removeClass();
        var pathname = window.location.pathname;
        var controller = $("#sidebar_cashier [href='" + pathname + "']");
        controller.parent('li').addClass('active');
        controller.parent('li').parents('li').addClass('open');
        if (pathname == '/index/Cashier/index') {
            $("#v-cashier").addClass('open');
        }
//        设置焦点
        $('#card_id').focus();
        printOrder(1);
        //支付方式
        set_pay_way();
    });

    function set_pay_way(){
        var $pay_select=$('select[name=pay_way]');
        if($pay_select.length==0){
            return;
        }
        $pay_select.html('');
        $.post('{:U("Pay/getWay")}',{},function(data){
            if(data.status=='error'){
                alert(data.data);
            }
            var $str='';
            $.each(data.data,function(i,item){
                $str+='<option value="'+item.way+'"';
                if(i==0){
                    $str+=' selected=selected ';
                }
                $str+='>'+item.remark+'</option>';
            });
            $pay_select.append($str);
        },'json');
    }
</script>